home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / ArrowCDEF / ArrowCDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-10  |  9.1 KB  |  264 lines  |  [TEXT/KAHL]

  1. /*******************************************************************************
  2.     FILE:        ArrowCDEF.c
  3.     CREATED:    March 16, 1994
  4.     AUTHOR:        David Hay
  5.     VERSION:    1.1
  6.     
  7.     Copyright © 1994  David Hay
  8.     
  9.     ArrowCDEF may be freely distributed, modified, included in any application
  10.     or CD-ROM compilation, etc. as long as the following restrictions are met:
  11.         (1)    This entire copyright notice remains intact when the source is
  12.             redistributed, modified, etc.
  13.         (2)    The copyright notice in the CDEF itself (the "copyright" string)
  14.             remains intact.
  15.  
  16.     David Hay hereby disclaims all warranties relating to this software,
  17.     whether express or implied, including without limitation any implied
  18.     warranties of merchantability or fitness for a particular purpose. David
  19.     Hay will not be liable for any special, incidental, consequential,
  20.     indirect or similar damages due to loss of data or any other reason, even
  21.     if David Hay or an agent of his has been advised of the possibility of
  22.     such damages.  In no event shall David Hay be liable for any damages,
  23.     regardless of the form of the claim.  The person using the software bears
  24.     all risk as to the quality and performance of the software.
  25.     
  26.     US Governement:
  27.         Government End Users:  If you are acquiring the Software and fonts
  28.     on behalf of any unit or agency of the United States Government, the
  29.     following provisions apply.  The Government agrees:
  30.         (i) if the Software and fonts are supplied to the Department of
  31.     Defense (DoD), the Software and fonts are classified as "Commercial
  32.     Computer Software" and the Government is acquiring only "restricted rights"
  33.     in the Software, its documentation and fonts as that term is defined in
  34.     Clause 252.227-7013(c)(1) of the DFARS; and
  35.         (ii) if the Software and fonts are supplied to any unit or agency
  36.     of the United States Government other than DoD, the Government's rights in
  37.     the Software, its documentation and fonts will be as defined in Clause
  38.     52.227-19(c)(2) of the FAR or, in the case of NASA, in Clause
  39.     18-52.227-86(d) of the NASA Supplement to the FAR.
  40.     
  41.     HOW TO USE ArrowCDEF:
  42.     
  43.     Implements an up-down arrow similar to those found in the color picker. It
  44.     uses a set of four picts to determine how to draw the CDEF in each of it's
  45.     four states. To use the CDEF, create an APIC resource that gives the
  46.     resource ID's of the four picts to use for the plain arrow, the arrow with
  47.     the up button pressed, the arrow with the down button pressed, and the
  48.     arrow when it is inactive. The ID of the APIC resource should be put in
  49.     the control's refCon field when the control is initialized. After the
  50.     control is created, the reference constant may be used however you wish.
  51.     
  52.     Comments and questions are welcome:
  53.     E-mail:        hay@cs.colorado.edu
  54.     US Mail:    David Hay
  55.                 117 Piedra Loop
  56.                 Los Alamos, NM 87544
  57.     
  58.     Thanks to Eddy J. Gurney and to all those that have expressed interest in
  59.     this CDEF. Also, thanks to the regulars on comp.sys.mac.programmer for
  60.     indirectly helping me untangle the mac toolbox by consistantly helping
  61.     beginners like myself.
  62.     
  63.     CHANGE HISTORY:
  64.         1.0        --    Initial release
  65.         1.1        --    Modified to used GetResource instead of GetPicture and
  66.                     Get1Resource so we don't need to keep track of the resource
  67.                     file. Also changed the license to one less restrictive.
  68. *******************************************************************************/
  69.  
  70. #include <SetUpA4.h>
  71. #include "ArrowCDEF.h"
  72.  
  73. #define    kInactive    255        /* part code indicating the control is inactive */
  74.  
  75. const unsigned char copyright[] = "Arrow CDEF 1.1 ©1994 David Hay.";
  76.  
  77. struct ControlResData        /* The data we get from the resource */
  78. {
  79.     short    plainPictID;    /* PICT to draw for an unhilited arrow            */
  80.     short    upPictID;        /* PICT to draw when the up arrow is pressed    */
  81.     short    downPictID;        /* PICT to draw then the down arrow is pressed    */
  82.     short    inactivePictID;    /* PICT to draw when the arrow is inactive        */
  83. };
  84.  
  85. typedef struct ControlResData ControlResData;
  86. typedef ControlResData **CntlResDataHndl;
  87.  
  88.  
  89. struct ControlData    /* the data we store in the contrlData field of the control */
  90. {
  91.     ControlResData        picts;    /* The PICTs to draw */
  92. };
  93.  
  94. typedef struct ControlData ControlData;
  95. typedef ControlData **ControlDataHandle;
  96.  
  97.  
  98.  
  99. pascal long main( short variation, ControlHandle theControl, short msg, long param )
  100. {
  101.     long                result;        /* the result to return */
  102.     ControlDataHandle    control;    /* the control data */
  103.  
  104.     RememberA0();
  105.     SetUpA4();
  106.  
  107.     result = 0;
  108.  
  109.     switch ( msg )
  110.     {
  111.         case initCntl:        /* initialize the arrow control data */
  112.         {
  113.             short            arrowResID;    /* ID of resource holding the pict info */
  114.             CntlResDataHndl    picts;        /* res handle to hold which picts to use */
  115.             Rect            picRect;    /* rectangle of the picture rect */
  116.             PicHandle        plainPict;    /* plain picture determines size of control */
  117.             
  118.             arrowResID = GetCRefCon( theControl );
  119.             picts = (CntlResDataHndl) GetResource( kArrowResType, arrowResID );
  120.             control = (ControlDataHandle) NewHandleClear( sizeof( ControlData ) );
  121.             BlockMove( *picts, *control, sizeof( ControlResData ) );
  122.             (*theControl)->contrlData = (Handle)control;
  123.             ReleaseResource( picts );    /*    We copied it so we don't need it anymore */
  124.             
  125.                 /*    Get the plain picture to determine the size of the control.
  126.                 **    After resizing the control to the plain picture, release
  127.                 **    the picture
  128.                 **/
  129.             plainPict = (PicHandle) GetResource( 'PICT',
  130.                                                  (*control)->picts.plainPictID );
  131.             picRect = (*plainPict)->picFrame;
  132.             SizeControl( theControl, picRect.right - picRect.left,
  133.                          picRect.bottom - picRect.top );
  134.             ReleaseResource( plainPict );
  135.             break;
  136.         }
  137.  
  138.         case drawCntl:        /* Draw the arrow control */
  139.         {
  140.             PicHandle        thePict;    /* the PICT to draw */
  141.             unsigned char    hilite;        /* the current control hilite state */
  142.             Rect            drawRect;    /* the rect to draw the PICT in */
  143.             short            pictID;        /* the resource ID of the PICT */
  144.             short            curRes;        /* the current resource file */
  145.             
  146.             if ( (*theControl)->contrlVis )
  147.             {
  148.                     /*    Initialize some stuff and grab some values out of the
  149.                     **    control structure so we don't have to constantly
  150.                     **    dereference the control handle
  151.                     **/
  152.                 thePict = NULL;
  153.                 hilite = (*theControl)->contrlHilite;
  154.                 control = (ControlDataHandle) (*theControl)->contrlData;
  155.                 drawRect = (*theControl)->contrlRect;
  156.                 switch( hilite )
  157.                 {
  158.                     case 0:                /*    no hiliting             */
  159.                         pictID = (*control)->picts.plainPictID;
  160.                         break;
  161.                     case inUpButton:    /*    up arrow hilited    */
  162.                         pictID = (*control)->picts.upPictID;
  163.                         break;
  164.                     case inDownButton:    /*    down arrow hilited    */
  165.                         pictID = (*control)->picts.downPictID;
  166.                         break;
  167.                     case kInactive:        /*    inactive control    */
  168.                         pictID = (*control)->picts.inactivePictID;
  169.                         break;
  170.                     default:            /* any other part codes don't apply */
  171.                         pictID = -1;
  172.                         break;
  173.                 }
  174.                 if ( pictID >= 0 )    /* This will probably always be true, but */
  175.                 {                    /* better safe than sorry! */
  176.                     thePict = (PicHandle) GetResource( 'PICT', pictID );
  177.                     if ( thePict )    /* We got the picture handle */
  178.                     {
  179.                         if ( hilite == kInactive )    /* Erase the arrow only when…    */
  180.                             EraseRect( &drawRect );    /* …it needs to be deactivated    */
  181.                         DrawPicture( thePict, &drawRect );
  182.                         ReleaseResource( thePict );
  183.                     }
  184.                 }
  185.             }
  186.             break;
  187.         }
  188.  
  189.         case testCntl:    /* Determine which part of the arrow the mouse is in */
  190.         {
  191.             Point    mousePoint;        /* where the mouse is */
  192.             Rect    upRect;            /* rect comprimising the up arrow */
  193.             Rect    downRect;        /* rect comprimising the down arrow */
  194.             Rect    plainRect;        /* the entire arrow rect */
  195.                         
  196.             plainRect = (*theControl)->contrlRect;
  197.             mousePoint.v = HiWord( param );
  198.             mousePoint.h = LoWord( param );
  199.  
  200.                 /*    If the mouse point is in the control and it is active, 
  201.                 **    determine which part the mouse went down in    
  202.                 **/
  203.             if ( (*theControl)->contrlHilite != kInactive && PtInRect( mousePoint, &plainRect ) )
  204.             {
  205.                     /*    Assume the up arrow and down arrow each take
  206.                     **    half the control
  207.                     **/
  208.                 upRect = plainRect;
  209.                 upRect.bottom -= (plainRect.bottom - plainRect.top) / 2;
  210.                 downRect = plainRect;
  211.                 downRect.top = upRect.bottom - 1;
  212.  
  213.                 if ( PtInRect( mousePoint, &upRect ) )    /* up arrow pressed */
  214.                 {
  215.                     result = inUpButton;
  216.                 }
  217.                 else if ( PtInRect( mousePoint, &downRect ) ) /* down arrow pressed */
  218.                 {
  219.                     result = inDownButton;
  220.                 }
  221.             }
  222.             break;
  223.         }
  224.         
  225.         case calcCRgns:    /* This is the message sent if using 24-bit addressing */
  226.             param = (long) StripAddress( (Ptr)param );    /* Mask off the high byte if necessary */
  227.             /*    FALL THROUGH ON PURPOSE!    */
  228.  
  229.         case calcCntlRgn:    /* Under 32-bit addressing we get this message */
  230.             RectRgn( (RgnHandle)param, &(*theControl)->contrlRect );
  231.             break;
  232.         
  233.         case dispCntl:        /* Free the data stored in the control */
  234.             if ( (*theControl)->contrlData );
  235.             {
  236.                 DisposeHandle( (*theControl)->contrlData );
  237.                 (*theControl)->contrlData = NULL;
  238.             }
  239.             break;
  240.  
  241.     #if 0    
  242.         case posCntl:
  243.         case thumbCntl:
  244.         case dragCntl:
  245.         case autoTrack:
  246.         case undoDev:
  247.         case cutDev:
  248.         case copyDev:
  249.         case pasteDev:
  250.         case clearDev:
  251.         case cursorDev:
  252.         case calcThumbRgn:
  253.             break;    /* We don't respond to any of these messages */
  254.     #endif
  255.  
  256.         default:    /* Any other messages we don't handle or which aren't defined yet */
  257.             break;
  258.     }
  259.  
  260.     RestoreA4();
  261.  
  262.     return( result );
  263. }
  264.